import { asyncRouterMap, constantRouterMap } from '@/config/router.config'
import { axios } from '@/utils/request'

/**
 * 过滤账户是否拥有某一个权限，并将菜单从加载列表移除
 * @param permission
 * @param route
 * @returns {boolean}
 */
function hasPermission (permission, route) {
  if (route.meta && route.meta.permission) {
    let flag = false
    for (let i = 0, len = permission.length; i < len; i++) {
      flag = route.meta.permission.includes(permission[i])
      if (flag) {
        return true
      }
    }
    return false
  }
  return true
}

/**
 * 单账户多角色时，使用该方法可过滤角色不存在的菜单
 * @param roles
 * @param route
 * @returns {*}
 */
// eslint-disable-next-line
function hasRole (roles, route) {
  if (route.meta && route.meta.roles) {
    return route.meta.roles.includes(roles.id)
  } else {
    return true
  }
}

/**
 * 递归过滤菜单权限
 * @param {*} routerMap
 * @param {*} roles
 */
// eslint-disable-next-line
function filterAsyncRouter (routerMap, roles) {
  const accessedRouters = routerMap.filter(route => {
    if (hasPermission(roles.permissionList, route)) {
      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children, roles)
      }
      return true
    }
    return false
  })
  return accessedRouters
}

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes ({ commit }, data) {
      return new Promise(async (resolve) => {
        const fchild = asyncRouterMap[0].children
        // 此处屏蔽权限控制
        // const { roles } = data
        // const accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
        const menus = await axios({
          url: '/manager/menu',
          method: 'get'
        }).then(res => {
          return res.data.menus
        })
        const menusData = []
        menus.map((item, index) => {
          menusData.push(item.path)
        })
        // let rm = [];
        fchild.map((item, index) => {
          if (menusData.indexOf(item.path) > -1) {
            if (item.children) {
              item.children.map((ii, ind) => {
                if (menusData.indexOf(ii.path) > -1) {
                  if (ii.children) {
                    ii.children.map((i, ina) => {
                      if (menusData.indexOf(i.path) === -1) {
                        delete ii.children[ina]
                      }
                    })
                  }
                } else {
                  delete item.children[ind]
                }
              })
            }
          } else {
            delete fchild[index]
          }
        })
        const realArr = []
        fchild.map(item => {
          if (item) realArr.push(item)
        })
        asyncRouterMap[0].children = realArr
        // 改写直接使用路由配置
        const accessedRouters = asyncRouterMap
        commit('SET_ROUTERS', accessedRouters)
        resolve()
      })
    }
  }
}

export default permission
